package cn.anecansaitin.whimsy.client.item.render.handcannon;

import cn.anecansaitin.whimsy.Whimsy;
import cn.anecansaitin.whimsy.common.item.handcannon.HandCannon;
import cn.anecansaitin.whimsy.common.register.ModDataAttachments;
import cn.anecansaitin.whimsy.common.register.ModItems;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import software.bernie.geckolib.cache.object.BakedGeoModel;
import software.bernie.geckolib.model.DefaultedItemGeoModel;
import software.bernie.geckolib.renderer.GeoItemRenderer;

public class HandCannonRender extends GeoItemRenderer<HandCannon> {
    public HandCannonRender() {
        super(new DefaultedItemGeoModel<>(Whimsy.resource("hand_cannon")));
    }

    @Override
    public void renderByItem(ItemStack stack, ItemDisplayContext transformType, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
        animatable = (HandCannon) stack.getItem();
        currentItemStack = stack;
        renderPerspective = transformType;
        if (transformType == ItemDisplayContext.GUI) {
            this.renderInGui(transformType, poseStack, bufferSource, packedLight, packedOverlay);
        } else {
            if (transformType == ItemDisplayContext.FIRST_PERSON_LEFT_HAND) {
                Minecraft mc = Minecraft.getInstance();
                ItemRenderer itemRenderer = mc.getItemRenderer();
                ItemStack arm = stack.getData(ModDataAttachments.HAND_CANNON_ARM).itemStack;

                BakedModel model = itemRenderer.getModel(arm, mc.level, null, 0);
                itemRenderer.render(arm, ItemDisplayContext.FIRST_PERSON_LEFT_HAND, true, poseStack, bufferSource, packedLight, packedOverlay, model);
            }

            RenderType renderType = getRenderType(animatable, getTextureLocation(animatable), bufferSource, Minecraft.getInstance().getFrameTime());
            VertexConsumer buffer = ItemRenderer.getFoilBufferDirect(bufferSource, renderType, false, this.currentItemStack != null && currentItemStack.hasFoil());
            defaultRender(poseStack, animatable, bufferSource, renderType, buffer, 0.0F, Minecraft.getInstance().getFrameTime(), packedLight);
        }
    }
}
